﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RN.ERP.Web.PersonService;
using RN.ERP.Web.ModelService;
using Newtonsoft.Json;

namespace RN.ERP.Web.Material.MateClass
{
    public partial class MateClassInfo : BasePage
    {
        PersonServiceClient client = new PersonServiceClient();
        private string QCPageCode = "QC";
        private string QCPage = "BmQCList.aspx";
        public string PageTitle = string.Empty;
        public string AppID
        {
            get
            {
                return HttpContext.Current.Application[ApplicationConstant.APPLICATION_ID].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ////type:0添加1查看2修改
                hidType.Value = Request.QueryString["type"];
                hidKMId.Value = Request.QueryString["kmid"];
                if (hidType.Value == "1")
                {
                    lbtnSave.Enabled = false;
                }
                ////绑定期次下拉
                BindDDL();
                BindData();
            }
        }

        protected void lbtnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(ddlQC.SelectedValue))
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "$.ligerDialog.warn('请选择期次！');", true);
            }
            else
            {
                Exam_KM examKm = new Exam_KM();
                OperationResult result = new OperationResult();
                result.Result = false;
                result.Message = "操作失败";
                if (!string.IsNullOrEmpty(hidKMId.Value))
                {
                    int kmId = 0;
                    int.TryParse(hidKMId.Value, out kmId);
                    examKm.ID = kmId;
                }
                examKm.Title = txtTitle.Text;
                examKm.QCId = int.Parse(ddlQC.SelectedValue);
                examKm.PassScore = float.Parse(txtPassScore.Text);
                examKm.IsrtDate = DateTime.Now;
                examKm.IsDel = false;
                
                int examLength = 0;
                if (int.TryParse(txtExamLength.Text, out examLength) && examLength > 0)
                {
                    examKm.ExamLength = examLength;
                }

                var km = JsonConvert.SerializeObject(examKm);
                if (hidType.Value == "0")
                {
                    result = JsonConvert.DeserializeObject<ModelService.OperationResult>(client.AddExamKM(km));
                }
                else if (hidType.Value == "2")
                {
                    result = JsonConvert.DeserializeObject<ModelService.OperationResult>(client.EditExamKM(km));
                }
                if (result.Result)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "", "$.ligerDialog.warn('" + result.Message + "','',CloseWin);", true);
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "", "$.ligerDialog.warn('" + result.Message + "');", true);
                }
            }
        }

        private void BindData()
        {
            if (!string.IsNullOrEmpty(hidKMId.Value))
            {
                int kmId = 0;
                int.TryParse(hidKMId.Value, out kmId);
                Exam_KM examKm = JsonConvert.DeserializeObject<Exam_KM>(client.GetExamKM(kmId));
                if (examKm != null)
                {
                    txtTitle.Text = examKm.Title;
                    txtPassScore.Text = examKm.PassScore.ToString();
                    ddlQC.SelectedValue = examKm.QCId.ToString();
                    txtExamLength.Text = (examKm.ExamLength ?? 0).ToString();
                }
            }
        }

        private void BindDDL()
        {
            using (PersonService.PersonServiceClient client = new PersonService.PersonServiceClient())
            {
                int count = 0;
                ddlQC.DataSource = JsonConvert.DeserializeObject<List<ModelService.Exam_QC>>(client.GetExamQCList
                    (QCPageCode, UserInfo.RoleId, QCPage, AppID, 0, 0, out count));
                ddlQC.DataValueField = "ID";
                ddlQC.DataTextField = "Title";
                ddlQC.DataBind();
                ddlQC.Items.Insert(0, new ListItem("--请选择--", ""));
            }
        }
    }
}